import {defineStore} from 'pinia'
import {
    changePassword,
    delUser,
    findAllUsers,
    getInfo,
    getUser,
    login,
    register,
    updateUser,
    withdraw
} from "@/api/user";
import {setToken} from "@/utils/auth";
import {useRouter} from "vue-router";
import {ElMessage} from "element-plus";

const router = useRouter();
// useStore 可以是 useUser、useCart 之类的任何东西
// 第一个参数是应用程序中 store 的唯一 id
export const userStore = defineStore('user', {
    state: () => {
        return {
            // 用户昵称
            user: {
                nick_name: undefined,
                is_admin: false
            },
            // 用户列表
            userList: [],
            // 用户详细信息
            userInfo: {}
        }
    },
    actions: {
        // 提现
        async withdraw(data) {
            return new Promise(async (resolve, reject) => {
                    withdraw(data).then(res => {
                        resolve(res)
                    }).catch(err => {
                        reject(err)
                    })
                }
            )
    },
    updatePassword(password, newPassword) {
        return new Promise(async (resolve, reject) => {
            changePassword({password, newPassword}).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    },
    addUser(userinfo) {
        return new Promise((resolve, reject) => {
            register(userinfo).then(res => {
                resolve(res.result)
            }).catch(err => {
                reject()
            })
        })
    },
    async getUserList(query) {
        return new Promise((resolve, reject) => {
            findAllUsers(query).then(res => {
                this.userList = res.result.list
                resolve(res.result)
            }).catch(err => {
                reject()
            })
        })
    },
    async getUserInfo(id) {
        const {result} = await getUser(id)
        this.userInfo = result
    },
    updateUser(data) {
        return new Promise(async (resolve, reject) => {
            updateUser(data).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    },
    async deleteUser(id) {
        const {code} = await delUser(id)
        if (code === 0) await this.getUserList()
        ElMessage.success("删除成功")
    },
    login(data) {
        return new Promise(async (resolve, reject) => {
            login(data).then(res => {
                this.nick_name = res.result.nick_name
                setToken(res.result.token)
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    },
    getInfo() {
        return new Promise((resolve, reject) => {
            getInfo().then(res => {
                this.user = res.result
                this.userInfo = res.result
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    }
}
})